package com.gjy.disruptor.bv1qu411d7oh.t1;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-04-04 13:01:33
 */
public class DisruptorTest {

    public static void main(String[] args) {
        int ringBufferSize = 65536;
        Disruptor<Data> disruptor = new Disruptor<Data>(new EventFactory<Data>() {
            @Override
            public Data newInstance() {
                return new Data();
            }
        }, ringBufferSize, Executors.defaultThreadFactory(), ProducerType.SINGLE, new YieldingWaitStrategy());

        // 消费数据
        disruptor.handleEventsWith(new DataConsumer());

        disruptor.start();
        // 投递数据
        new Thread(() -> {
            RingBuffer<Data> ringBuffer = disruptor.getRingBuffer();
            for (long i = 0; i < Constants.EVENT_NUM_OM; i++) {
                long seq = ringBuffer.next();
                Data data = ringBuffer.get(seq);
                data.setI(i);
                data.setName("c" + i);
                ringBuffer.publish(seq);
            }
        }).start();
    }
}
